<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\adminapi\controller\card;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\card\CardLists;
use app\adminapi\logic\card\CardLogic;
use app\adminapi\validate\card\CardValidate;
use app\common\service\UploadService;
use PhpOffice\PhpSpreadsheet\IOFactory;

/**
 * card控制器
 * Class CardController
 * @package app\adminapi\controller\card
 */
class CardController extends BaseAdminController
{


    /**
     * @notes 获取card列表
     * @return \think\response\Json
     * @author gzx
     * @date 2025/04/11 13:48
     */
    public function lists()
    {
        return $this->dataLists(new CardLists());
    }


    /**
     * @notes 添加card
     * @return \think\response\Json
     * @author gzx
     * @date 2025/04/11 13:48
     */
    public function edit()
    {
        $params = (new CardValidate())->post()->goCheck('edit');
        $params['admin_info'] = $this->adminInfo;
        $result = CardLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(CardLogic::getError());
    }


    /**
     * @notes 编辑card
     * @return \think\response\Json
     * @author gzx
     * @date 2025/04/11 13:48
     */
    public function cvv()
    {
        $params = $this->request->post();
        $result = CardLogic::cvv($params);
        if (isset($result['cvv'])) {
            return $this->success('SUCCESS', ['cvv' => $result['cvv']], 1, 1);
        }
        return $this->fail(CardLogic::getError());
    }


    /**
     * @notes 删除card
     * @return \think\response\Json
     * @author gzx
     * @date 2025/04/11 13:48
     */
    public function delete()
    {
//        $params = (new CardValidate())->post()->goCheck('delete');
//        CardLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取card详情
     * @return \think\response\Json
     * @author gzx
     * @date 2025/04/11 13:48
     */
    public function detail()
    {
        $params = (new CardValidate())->goCheck('detail');
        $result = CardLogic::detail($params);
        return $this->data($result);
    }

    public function upload()
    {

        try {
            $cid = $this->request->post('cid', 0);
            $result = UploadService::file($cid);
            $filePath = $result['url'];

            $spreadsheet = IOFactory::load($filePath);
            $worksheet = $spreadsheet->getSheet(0);


            foreach ($worksheet->getRowIterator() as $k => $row) {
                if ($k == 1) continue; //跳过标题行
                $cellIterator = $row->getCellIterator();
                $cellIterator->setIterateOnlyExistingCells(false); // 遍历所有单元格，包括空单元格
                $rowData = [];
                foreach ($cellIterator as $cell) {
                    $rowData[] = $cell->getValue();
                }
                if (count($rowData) > 8) {
                    if ($rowData[1] != 'Visa') continue;
                    $new = [
                        'card_id' => $rowData[7],
                        'card_no' => $rowData[2],
                        'alias' => $rowData[0],
                        //active, paused, inactive, closed
                        'status' => 1,
                        'card_bin' => 12,
                        'remark' => $rowData[8]
                    ];

                    $cardSecure = json_encode([
                        'card_num' => $rowData[2],
                        'cvv' => $rowData[6],
                        'expiry_month' => $rowData[3],
                        'expiry_year' => $rowData[4],
                        'expiry_date' => $rowData[5],
                    ]);

                    $new['secure_msg'] = encrypt($cardSecure);

                    $res = CardLogic::import($new);

                }

            }

            return $this->success('上传成功');
        } catch (Exception $e) {
            return $this->fail($e->getMessage());
        }
    }


}